<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyWord" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAddbui">添加楼栋</el-button>
				</el-form-item>
        <el-form-item>
					<el-button type="primary" @click="handleAddunit">添加单元</el-button>
				</el-form-item>
        <el-form-item>
					<el-button type="primary" @click="handleAddhouse">添加房屋</el-button>
				</el-form-item>
			</el-form>
		</el-col>


    <!--列表-->
    <el-table :data="employees"  highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="number" label="房屋编号" width="120">
      </el-table-column>
      <el-table-column prop="floor" label="房屋楼层" width="150">
      </el-table-column>
      <el-table-column prop="unit.unitNumber" label="单元" width="150">
      </el-table-column>
      <el-table-column prop="building.number" label="楼栋" width="100">
      </el-table-column>
      <el-table-column prop="owner.name" label="业主" width="100">
      </el-table-column>

      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


		<!--工具条(分页,批量删除)-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total"
                     :current-page="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑添加界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="房屋编号" prop="number">
					<el-input v-model="editForm.number" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="房屋楼层" prop="floor">
					<el-input v-model="editForm.floor" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="单元">
          <el-select v-model="editForm.unitId" placeholder="请选择">
            <el-option
                v-for="item in units"
                :label="item.unitNumber"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋">
          <el-select v-model="editForm.buildingId" placeholder="请选择">
            <el-option
                v-for="item in buildings"
                :label="item.number"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
    <!--添加楼栋界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible1" :close-on-click-modal="false">
			<el-form :model="AddBuilding" label-width="80px" :rules="editFormRules" ref="AddBuilding">
				<el-form-item label="楼栋编号" prop="number">
					<el-input v-model="AddBuilding.number" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="楼栋名称" prop="name">
					<el-input v-model="AddBuilding.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="楼栋面积" prop="floorArea">
					<el-input v-model="AddBuilding.floorArea" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="备注" prop="remarks">
					<el-input v-model="AddBuilding.remarks" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible1 = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmitBuilding" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
    <!--添加单元界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible2" :close-on-click-modal="false">
			<el-form :model="AddUnit" label-width="80px" :rules="editFormRules" ref="AddUnit">
				<el-form-item label="单元编号" prop="unitNumber">
					<el-input v-model="AddUnit.unitNumber" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="总层数" prop="unitLayers">
					<el-input v-model="AddUnit.unitLayers" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="建筑面积" prop="floorArea">
					<el-input v-model="AddUnit.floorArea" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="是否有电梯：有1 || 无0" prop="elevatorType">
					<el-input v-model="AddUnit.elevatorType" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="楼栋">
          <el-select v-model="AddUnit.buidingId" placeholder="请选择">
            <!--
            :label:我们选中后 显示的结果
            :value：我们要传给后端的字段
            -->
            <el-option
                v-for="item in buildings"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.number }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
					<el-input v-model="AddUnit.remarks" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible2 = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmitUnit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
    <!--添加房屋界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible3" :close-on-click-modal="false">
			<el-form :model="AddHouse" label-width="80px" :rules="editFormRules" ref="AddHouse">
				<el-form-item label="房屋编号" prop="number">
					<el-input v-model="AddHouse.number" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="房屋楼层" prop="floor">
					<el-input v-model="AddHouse.floor" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="房屋类型" prop="housingType">
					<el-input v-model="AddHouse.housingType" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="建筑面积" prop="buildingArea">
					<el-input v-model="AddHouse.buildingArea" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="室内面积" prop="indoorArea">
					<el-input v-model="AddHouse.indoorArea" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="是否精装修： 是1 || 否0" prop="decorationType">
					<el-input v-model="AddHouse.decorationType" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="楼栋">
          <el-select v-model="AddHouse.buildingId" placeholder="请选择">
            <el-option
                v-for="item in buildings"
                :label="item.name"
                :value="item.id">

              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单元">
          <el-select v-model="AddHouse.unitId" placeholder="请选择">
            <el-option
                v-for="item in Unitbuils"
                :label="item.unitNumber"
                :value="item.id">
              <span style="float: left">{{ item.unitNumber }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.building.name}}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remarks">
					<el-input v-model="AddHouse.remarks" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible3 = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmitHouse" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

	export default {
		data() {
			return {
			  //抽屉数据
        // gridData:[],
        //默认不弹出抽屉模态框
        // table:false,

        currentPage:1,
        pageSize:5,

        //关键字
				filters: {
					keyWord: ''
				},
				employees: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
        editFormVisible1: false,//编辑界面是否显示
        editFormVisible2: false,//编辑界面是否显示
        editFormVisible3: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					number: [
						{ required: true, message: '请输入房屋号', trigger: 'blur' }
					]
				},
				//编辑和添加界面数据
				editForm: {
					id: 0,
					number: '',
					floor: 0,
          unitId: 0,
          buildingId: 0,
				},
        AddBuilding:{
          id: 0,
          number: '',
          name: '',
          floorArea: 0,
          remarks: '',
        },
        AddUnit:{
          unitNumber:'',
          unitLayers:0,
          floorArea: 0,
          elevatorType: 0,
          buidingId: 0,
          remarks: '',
        },
        AddHouse:{
          number:'',
          floor:0,
          housingType: '',
          buildingArea: 0,
          indoorArea: 0,
          decorationType: 0,
          unitId: 0,
          buildingId: 0,
          ownerId: 0,
          remarks: '',
        },
        title:'',
        // options:''


			}
		},
		methods: {
			//性别显示转换
			// formatSex: function (row, column) {
			// 	return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			// },
      //分页页数点击
			handleCurrentChange(val) {
        this.currentPage = val;
				this.getEmployees();
			},
      //查询,查出来的数据从第一页开始展示,而不是当前页
      search() {
        this.currentPage = 1;
				this.getEmployees();
			},
			//获取员工列表
			getEmployees() {
				let para = {
					pageSize: this.pageSize,
					currentPage: this.currentPage,
          keyWord: this.filters.keyWord
				};
				this.listLoading = true;
				this.$http.post("/house/list",para)
        .then(result=>{
          //关闭忙等
          this.listLoading = false;
          this.employees=result.data.rows
          this.total=result.data.total

        })
        .catch(result=>{
          this.$message({
            message: '网络不通',
            type: 'error'
          });
        })

			},
      //获取单元数据
      getUnits(){
			  this.$http.get("/unit")
        .then(Result=>{
          this.units=Result.data
        })
      },
      //获取楼栋数据
      getBuildings(){
			  this.$http.get("/building")
        .then(Result=>{
          this.buildings=Result.data
        })
      },
      //获取楼栋单元数据
      getUnitbuil(){
        this.$http.get("/unit/buil")
            .then(Result=>{
              this.Unitbuils=Result.data
            })
        console.log(this.Unitbuils)
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					console.log(row)
					this.$http.delete("/house/"+row.id)
          .then(result=>{
              result = result.data
            if (result.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }else {
              this.$message({
                message: '删除失败',
                type: 'error'
              });
            }
            this.listLoading = false;
            this.currentPage=1;
            this.getEmployees();
          })

          .catch(result=>{
            this.$message({
              message: '网络不通',
              type: 'error'
            });

          })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="编辑";
				this.editFormVisible = true;
				//直接传递为地址传递 this.editForm = row,导致编辑表单后点击取消也会进行修改
				this.editForm = Object.assign({}, row);

			},
			// //显示新增界面
			// handleAdd: function () {
			//   this.title="新增";
			// 	this.editFormVisible = true;
			// 	this.editForm = {
			// 	};
			// },
      //显示新增楼栋界面
      handleAddbui: function () {
			  this.title="添加楼栋";
				this.editFormVisible1 = true;
				this.AddBuilding = {
				};
			},
      //显示新增单元界面
      handleAddunit: function () {
			  this.title="添加单元";
				this.editFormVisible2 = true;
				this.AddUnit = {
				};
			},
      //显示新增房屋界面
      handleAddhouse: function () {
			  this.title="添加房屋";
				this.editFormVisible3 = true;
				this.AddHouse = {
				};
			},
			//编辑和新增
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
				  //判断输入框内是否有数据
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
              //将表单中的数据赋值给空对象 , 直接传递为值传递 this.
							let para = Object.assign({}, this.editForm);
              // // 新增的时候 parentId 传的是数组 但是 编辑的时候 它传的是一个 long
              // if (para.parentId && para.parentId instanceof Array){
              //   // 拿到 parentId 数组的最后一个数值
              //   para.parentId = para.parentId[para.parentId.length - 1]
              // }
							this.$http.put("/house",para)
                  .then(result=>{
                    result = result.data
                    if (result.success){
                      this.$message({
                        message: '操作成功',
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: '操作失败',
                        type: 'error'
                      });
                    }
                    this.editLoading = false;
                    this.currentPage=1;
                    this.getEmployees();
                    this.editFormVisible = false;
                  })

                  .catch(result=>{
                    this.$message({
                      message: '网络不通',
                      type: 'error'
                    });
                    this.editLoading = false;
                    this.editFormVisible = false;
                  })

						});
					}
				});
			},
      //新增楼栋
      editSubmitBuilding: function () {
				this.$refs.AddBuilding.validate((valid) => {
				  //判断输入框内是否有数据
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.AddBuilding);
							console.log(para)
							this.$http.put("/building",para)
                  .then(result=>{
                    result = result.data
                    if (result.success){
                      this.$message({
                        message: '操作成功',
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: '操作失败',
                        type: 'error'
                      });
                    }
                    this.editLoading = false;
                    this.currentPage=1;
                    this.getEmployees();
                    this.editFormVisible1 = false;
                  })

                  .catch(result=>{
                    this.$message({
                      message: '网络不通',
                      type: 'error'
                    });
                    this.editLoading = false;
                    this.editFormVisible1 = false;
                  })

						});
					}
				});
			},
      //新增单元
      editSubmitUnit: function () {
				this.$refs.AddUnit.validate((valid) => {
				  //判断输入框内是否有数据
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.AddUnit);
							console.log(para)
							this.$http.put("/unit",para)
                  .then(result=>{
                    result = result.data
                    if (result.success){
                      this.$message({
                        message: '操作成功',
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: '操作失败',
                        type: 'error'
                      });
                    }
                    this.editLoading = false;
                    this.currentPage=1;
                    this.getEmployees();
                    this.editFormVisible2 = false;
                  })

                  .catch(result=>{
                    this.$message({
                      message: '网络不通',
                      type: 'error'
                    });
                    this.editLoading = false;
                    this.editFormVisible2 = false;
                  })

						});
					}
				});
			},
      //新增房屋
      editSubmitHouse: function () {
				this.$refs.AddHouse.validate((valid) => {
				  //判断输入框内是否有数据
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.AddHouse);
							console.log(para)
							this.$http.put("/house",para)
                  .then(result=>{
                    result = result.data
                    if (result.success){
                      this.$message({
                        message: '操作成功',
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: '操作失败',
                        type: 'error'
                      });
                    }
                    this.editLoading = false;
                    this.currentPage=1;
                    this.getEmployees();
                    this.editFormVisible3 = false;
                  })

                  .catch(result=>{
                    this.$message({
                      message: '网络不通',
                      type: 'error'
                    });
                    this.editLoading = false;
                    this.editFormVisible3 = false;
                  })

						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
			  //获取所点击的数据的id的集合
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/house",ids)
              .then(result=>{
                result = result.data
                if (result.success){
                  this.$message({
                    message: '删除成功',
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: '删除失败',
                    type: 'error'
                  });
                }
                this.listLoading = false;
                this.currentPage=1;
                this.getEmployees();
              })

				}).catch(() => {

				});
			},

		},
		mounted() {
			this.getEmployees();
			this.getUnits();
			this.getBuildings();
      this.getUnitbuil()
			// this.getParent();
		}
	}

</script>

<style scoped>

</style>